package client.comm.baoding.fragment

import android.view.View
import client.comm.baoding.R
import client.comm.baoding.adapter.JifenAdapter
import client.comm.baoding.adapter.PendReleaseBalanceAdapter
import client.comm.baoding.api.bean.HmjlRet
import client.comm.baoding.databinding.FragmentLoadmorepageBinding
import client.comm.baoding.dialog.MsgTitleDialog
import client.comm.baoding.ui.vm.JifenViewModel
import client.comm.baoding.ui.vm.PendReleaseBalanceViewModel
import client.comm.commlib.base.BaseFragment
import client.comm.commlib.comm_ui.draw.SpaceItemDecoration
import client.comm.commlib.toast.toast

class PendReleaseBalanceOneFragment : BaseFragment<FragmentLoadmorepageBinding, PendReleaseBalanceViewModel>() {
    override fun getViewId(): Int {
        return R.layout.fragment_loadmorepage
    }

    val adapter: PendReleaseBalanceAdapter by lazy {
        PendReleaseBalanceAdapter(requireContext(), mImgLoader).apply {
            releaseRecord = {
                toast("释放记录")
            }
        }
    }

    override fun initViewModel(): PendReleaseBalanceViewModel = createViewModel()

    override fun initWidget() {

        binding.emptyLayout.setOnClickListener {
            viewModel.pageOne.value = 0
            viewModel.holdOne()
        }
        binding.recyclerView.addItemDecoration(SpaceItemDecoration(10))

        binding.recyclerView.adapter = adapter
        binding.recyclerView.onLoadMore = {
            if (adapter.hasMoreState.value!! && !viewModel.isOneLoading.value!!) {
                viewModel.holdOne()
            }
        }
        binding.refreshLayout.setOnRefreshListener {
            adapter.hasMoreState.value = true

            viewModel.pageOne.value = 0
            viewModel.holdOne()
        }
        viewModel.isOneLoading.observe(this, {
            if (!it) {
                binding.refreshLayout.isRefreshing = false
            }
        })

        viewModel.callOne.observe(this, {
//            it?.let {
//                if (it.isSuccess) {
//                    val list =
//                        it.data?.coin_list as MutableList<HmjlRet.Coin?>?
//                    if (viewModel.pageOne.value == 1) {
//                        adapter.submitList(list)
//                    } else
//                        adapter.appendList(list)
//                    if (list != null && list.size > 0) {
//                        if (list.size < 10) {
//                            adapter.hasMoreState.value = false
//                        }
//                        binding.emptyLayout.visibility = View.GONE
//                    } else {
//                        if (adapter.itemCount == 0)
//                            binding.emptyLayout.errorType = EmptyLayout.NODATA
//                        adapter.hasMoreState.value = false
//                    }
//                } else {
//                    binding.emptyLayout.errorType = EmptyLayout.NETWORK_ERROR
//                }
//            }


        })
    }

    override fun initData() {
        viewModel.pageOne.value = 1
//        viewModel.holdOne()

        //以下为测试数据
        val mList2: ArrayList<HmjlRet.Coin> = arrayListOf()
        mList2.add(HmjlRet.Coin("", "", 0, 0.0, 0.0, 0, 0.0, 0))
        mList2.add(HmjlRet.Coin("", "", 0, 0.0, 0.0, 0, 0.0, 0))

        adapter.submitList(mList2)
//        adapter.appendList(mList2)
        binding.emptyLayout.visibility = View.GONE
        adapter.hasMoreState.value = false


    }


}